import React from 'react';
import _ from 'lodash';
import router from 'umi/router';
import { Tooltip } from 'antd';
import { TextWithTags, NumberView } from '@/components/BaseTable';
import HeaderRenderer from '@/components/BaseTable/HeaderRenderer';

export const hasChildren = (list, item) => {
  let result = false;
  list.forEach(value => {
    if (value.pid === item.id) {
      result = true;
    }
  });
  return result;
};

export const getExpandedRowKeys = list => {
  const expandedRowKeys = [];
  _.forEach(list, value => {
    expandedRowKeys.push(value.id);
  });
  return expandedRowKeys;
};

export const buildTree = (list, pid = '0') => {
  const tree = [];
  list.forEach(value => {
    const x = value;
    if (x.pid === pid) {
      if (hasChildren(list, x)) {
        x.children = [];
        x.children = buildTree(list, x.id);
      }
      tree.push(x);
    }
  });
  return tree;
};

export const levelTree = (list, level) => {
  const cloneList = _.cloneDeep(list);
  let filterItem = [];
  if (level === 'region') {
    filterItem = _.filter(
      cloneList,
      v => v.type === 'org_wanda_group' || v.type === 'org_business_division',
    );
  }
  if (level === 'cityCompany') {
    filterItem = _.filter(
      cloneList,
      v =>
        v.type === 'org_wanda_group' ||
        v.type === 'org_business_division' ||
        v.type === 'org_city_company',
    );
  }
  if (level === 'project') {
    filterItem = _.filter(
      cloneList,
      v =>
        v.type === 'org_wanda_group' ||
        v.type === 'org_business_division' ||
        v.type === 'org_city_company' ||
        v.type === 'org_project',
    );
  }
  if (level === 'stage') {
    filterItem = _.filter(
      cloneList,
      v =>
        v.type === 'org_wanda_group' ||
        v.type === 'org_business_division' ||
        v.type === 'org_city_company' ||
        v.type === 'org_project' ||
        v.type === 'org_stage',
    );
  }
  return filterItem;
};

export const getArrayNumbers = (text, division = 1) => {
  if (!_.isEmpty(text) && text.indexOf('/') > -1) {
    // text.split('/')
    const temp = [];
    _.forEach(text.split('/'), value => {
      temp.push((_.trim(value) * 1) / division);
    });
    text = temp;
  } else {
    text = (text * 1) / division;
  }

  return text;
};

export const getFixedColumns = (titleTree, attributesCheckeds) => {
  const fixedColumns = [];
  fixedColumns.push({
    key: 'name',
    dataKey: 'name',
    title: '',
    width: 260,
    frozen: 'left',
    cellRenderer: ({ rowData }) => (
      <Tooltip placement="topLeft" title={rowData.name}>
        <div
          style={{
            width: '100%',
            textOverflow: 'ellipsis',
            whiteSpace: 'nowrap',
            overflow: 'hidden',
          }}
        >
          {rowData.name}
        </div>
      </Tooltip>
    ),
  });
  _.each(titleTree, item => {
    _.each(item.children, v => {
      if (_.indexOf(attributesCheckeds, '套数') > -1) {
        fixedColumns.push({
          key: `${v.key}-Num`,
          dataKey: `${v.key}-Num`,
          title: '',
          width: 180,
          align: 'right',
          headerRenderer: () => (
            <HeaderRenderer type={1}>
              <div>套数/车位数</div>
              <div>(套/个)</div>
            </HeaderRenderer>
          ),
          dataGetter: ({ rowData }) => [rowData[v.key].roomNum * 1, rowData[v.key].parkingNum * 1],
          cellRenderer: <NumberView precision={0} />,
        });
      }
      if (_.indexOf(attributesCheckeds, '面积') > -1) {
        fixedColumns.push({
          key: `${item.key + v.key}area`,
          dataKey: `${item.key + v.key}area`,
          title: '',
          width: 180,
          align: 'right',
          headerRenderer: () => (
            <div>
              <div>面积</div>
              <div>(平米)</div>
            </div>
          ),
          dataGetter: ({ rowData }) => [rowData[v.key].saleArea * 1],
          cellRenderer: <NumberView precision={2} />,
        });
      }
      if (_.indexOf(attributesCheckeds, '均价') > -1) {
        fixedColumns.push({
          key: `${item.key + v.key}averagePrice`,
          dataKey: `${item.key + v.key}averagePrice`,
          title: '',
          width: 180,
          align: 'right',
          headerRenderer: () => (
            <HeaderRenderer type={2}>
              <div>均价</div>
              <div>(元/平米,个)</div>
            </HeaderRenderer>
          ),
          dataGetter: ({ rowData }) => [
            rowData[v.key].houseAvgPrice * 1,
            rowData[v.key].parkingAvgPrice * 1,
          ],
          cellRenderer: <NumberView precision={0} />,
        });
      }
      if (_.indexOf(attributesCheckeds, '金额') > -1) {
        fixedColumns.push({
          key: `${item.key + v.key}price`,
          dataKey: `${item.key + v.key}price`,
          title: '',
          width: 280,
          align: 'right',
          headerRenderer: () => (
            <HeaderRenderer type={3}>
              <div>金额</div>
              <div>(万元：合计/房源/车位)</div>
            </HeaderRenderer>
          ),
          dataGetter: ({ rowData }) => [
            rowData[v.key].housePrice / 10000 + rowData[v.key].parkingPrice / 10000,
            rowData[v.key].housePrice / 10000,
            rowData[v.key].parkingPrice / 10000,
          ],
          cellRenderer: <NumberView precision={0} />,
        });
      }
    });
  });
  return fixedColumns;
};

export const getTitleTree = (attributesCheckeds, deadline) => {
  const year = deadline.split('-')[0];
  const month = deadline.split('-')[1];
  const titleTree = [
    {
      name: '组织/项目/分期',
      key: 'id',
      colspan: 1,
      rowspan: 4,
    },
    {
      name: `${year}年${month}月供货明细`,
      key: 'gonghuomingxi',
      rowspan: 1,
      children: [
        {
          name: '月初存货',
          key: 'inventory',
          colspan: 1 * attributesCheckeds.length,
        },
        {
          name: '本月计划供货',
          key: 'plan',
          colspan: 1 * attributesCheckeds.length,
        },
        {
          name: '本月实际供货',
          key: 'actual',
          colspan: 1 * attributesCheckeds.length,
        },
      ],
    },
  ];
  return titleTree;
};
